<script setup lang="ts">
import { ref } from 'vue'
import { RouterView,useRouter } from 'vue-router'
import { Menu as IconMenu, Promotion, Reading, Document } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores'
import { jwtDecode } from 'jwt-decode'
import type { JwtPayload } from '@/types/public'
import { ElMessageBox } from 'element-plus'
const isCollapse = ref(false)
const router = useRouter()

const userStore = useUserStore()
const decode = jwtDecode<JwtPayload>(userStore.token)
console.log(decode)
const userRole = decode['http://schemas.microsoft.com/ws/2008/06/identity/claims/role']
const isStudent = userRole === 'Student'
const isTeacher = userRole === 'Teacher'
const isAdmin = userRole === 'Admin'
const userName = decode['http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name']
const logout = async () => {
  // 退出操作
  await ElMessageBox.confirm('你确认要进行退出么', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消',
  })
  // 清除token
  userStore.removeToken()
  router.push('/login')
}
</script>

<template>
  <!--
    el-menu 整个菜单组件
      :default-active="$route.path"  配置默认高亮的菜单项
      router  router选项开启，el-menu-item 的 index 就是点击跳转的路径

    el-menu-item 菜单项
      index="/article/channel" 配置的是访问的跳转路径，配合default-active的值，实现高亮
  -->
  <el-container class="layout-container">
    <el-aside width="200px">
      <div class="el-aside__logo"></div>
      <el-menu
        :default-active="$route.path"
        class="el-menu-vertical"
        :collapse="isCollapse"
        background-color="#232323"
        text-color="#fff"
        active-text-color="#ffd04b"
      >
        <!-- 老师菜单 -->
        <el-menu v-if="isTeacher" active-text-color="#ffd04b" background-color="#232323" :default-active="$route.path" text-color="#fff" router>
          <el-menu-item index="/teacher/qrcode">
            <el-icon><Promotion /></el-icon>
            <span>课程二维码发布</span>
          </el-menu-item>

          <el-menu-item index="/teacher/course">
            <el-icon><Reading /></el-icon>
            <span>课程管理</span>
          </el-menu-item>

          <el-menu-item index="/teacher/attendance">
            <el-icon><Document /></el-icon>
            <span>考勤记录与统计</span>
          </el-menu-item>
        </el-menu>

        <!-- 学生菜单 -->
        <el-menu v-else-if="isStudent" active-text-color="#ffd04b" background-color="#232323" :default-active="$route.path" text-color="#fff" router>
          <el-menu-item index="/student/course">
            <el-icon><Reading /></el-icon>
            <span>课程记录</span>
          </el-menu-item>
        </el-menu>

        <!-- 管理员菜单 -->
        <el-menu v-else-if="isAdmin" active-text-color="#ffd04b" background-color="#232323" :default-active="$route.path" text-color="#fff" router>
          <el-menu-item index="/admin/class">
            <el-icon><Reading /></el-icon>
            <span>班级管理</span>
          </el-menu-item>
        </el-menu>
      </el-menu>
    </el-aside>
    <el-container>
      <el-header height="60px">
        <div class="header-left">
          <el-icon class="toggle-button" @click="isCollapse = !isCollapse">
            <IconMenu />
          </el-icon>
        </div>
        <div class="header-center" style="flex: 1; text-align: center">
          <span>欢迎您，{{ userName }}</span>
        </div>
        <div class="header-right">
          <el-button type="danger" @click="logout">退出登录</el-button>
        </div>
      </el-header>
      <el-main>
        <RouterView />
      </el-main>
    </el-container>
  </el-container>
</template>

<style lang="scss">
/* 添加全局样式重置 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.el-container {
  height: 100vh;
  width: 100%;
}

.el-aside {
  width: auto !important;
  .el-menu {
    border-right: none;
  }
}

.el-menu:not(.el-menu--collapse) {
  width: 200px;
}

.el-menu--collapse {
  width: 64px;
}
</style>

<style lang="scss" scoped>
.layout-container {
  width: 100%;
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;

  .el-aside {
    height: 100%;
    background-color: #232323;
    transition: width 0.3s;
  }

  .el-container {
    width: calc(100% - 200px);
    height: 100%;
  }

  .el-header {
    background-color: #fff;
    border-bottom: 1px solid #e6e6e6;
    display: flex;
    align-items: center;

    .header-left {
      .toggle-button {
        font-size: 20px;
        cursor: pointer;
        &:hover {
          color: #409eff;
        }
      }
    }
  }
}
</style>
